package com.zjitc.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjitc.common.Gsonal;
import com.zjitc.common.PageModel;
import com.zjitc.common.redis.RedisCache;
import com.zjitc.dao.CategoryDao;
import com.zjitc.dao.ProductDao;
import com.zjitc.pojo.Category;
import com.zjitc.pojo.History;
import com.zjitc.pojo.Product;
import com.zjitc.service.ProductService;
import com.zjitc.utils.RedisAuto;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

/**
 * Create By IntelliJ IDEA
 *
 * @author:
 * @create-Time: 2017/11/15  10:57
 */
@Service
public class ProductServiceImpl implements ProductService {

  @Autowired
  private ProductDao dao;

  @Autowired
  private CategoryDao cdao;

  @Autowired
  private RedisCache redis;
  private final String HOTS = "Hots";
  private final String PRODUCTS = "products";
  private final String NEWS = "News";
  private final String INVENTORY = "inventory";
    @Override
    public List<Product> findHots() {
            return dao.findHots();
    }

    @Override
    public List<Product> findNews() {
        return dao.findNews();
    }

    @Override
    public Product findbyid(String pid) {
        Product product = dao.findbypid(pid);
        Category findbycid = cdao.findbycid(product.getCid());
        product.setCategory(findbycid);

        return product;
    }

    @Override
    public PageInfo findall(String cid, int pageid, int count) {
        PageHelper.startPage(pageid,count);
        List<Product> bycid = dao.findBycid(cid);
        PageInfo info = new PageInfo(bycid);
        return info;
    }

    @Override
    public PageModel<Product> findall(int pageid, int count) {
      checkredis();
      List<Product> products = redis.hashListGetAll(PRODUCTS, Product.class);

      PageModel pageModel = new PageModel(products, count, pageid);
      return pageModel;
    }

    @Override
    public List<Product> findall() {
        return dao.findall();
    }


    @Override
    public ModelAndView displaymsg(HttpSession session,String pid) {
        if (!redis.exists(PRODUCTS)) {
            RedisAuto.productredis(dao,cdao,redis,PRODUCTS);
        }
        ModelAndView mc = new ModelAndView();
        String tojson = redis.hashMapget(PRODUCTS, pid);

        System.out.println(tojson);

        Product product = Gsonal.fromJson(tojson, Product.class);
        mc.addObject("product", product);
        makeHistory(session,product);
        mc.setViewName("product_info");
        return mc;
    }

  @Override
  public int updateProduct(Product product) {

    int update = dao.update(product);
    System.out.println("在service中的Product是" + product);
    Product findbypid = dao.findbypid(product.getPid());
    System.out.println("在数据库中的Product是：" + findbypid);
    RedisAuto.productredis(dao, cdao, redis, PRODUCTS);
    return update;
  }

  @Override
  public PageModel<Product> findAllProduct(int id, int count) {
    checkredis();
    List<Product> products = redis.hashListGetAll(PRODUCTS, Product.class);

    PageModel pageModel = new PageModel(products, count, id);
    return pageModel;
  }

  @Override
  public int addproduct(Product product) {
    redis.hashset(PRODUCTS,product.getPid(),Gsonal.tojson(product));
    int i = dao.addnewProduct(product);
    return i;
  }

  @Override
  public void deleteByPid(String pid) {
    redis.delHash(PRODUCTS,pid);
    dao.deletebypid(pid);
    }

  @Override
  public int updateInventory(String pid, int i) {
    Product product = redis.hashgetObject(PRODUCTS, pid, Product.class);
    product.setInventory(i);
    redis.hashset(PRODUCTS,pid,Gsonal.tojson(product));
    return dao.update(product);
  }

  private void checkredis() {
    if (!redis.exists(PRODUCTS)) {
      RedisAuto.productredis(dao, cdao, redis, PRODUCTS);
    }
    if (!redis.exists(HOTS)) {
      RedisAuto.hotsredis(dao, redis, HOTS);
    }
    if (!redis.exists(NEWS)) {
      RedisAuto.newsredis(dao, redis, NEWS);
    }
    RedisAuto.inventoryredis(dao,redis,INVENTORY);
  }



  private void makeHistory(HttpSession session, Product product){
        Object o = session.getAttribute("history");
        History history = null;

        if(o == null){
            history = new History();
            session.setAttribute("history",history);
        }else{
            history = (History) o;
        }
        history.add(product);
    }
}
